package com.tangcco.lockwell.controller.student;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.tangcco.lockwell.base.RestResponse;
import com.tangcco.lockwell.context.WebContext;
import com.tangcco.lockwell.domain.WrongQuestion;
import com.tangcco.lockwell.domain.dto.politics.QuestionPoliticsChildInfoDto;
import com.tangcco.lockwell.domain.dto.politics.QuestionPoliticsInfoDto;
import com.tangcco.lockwell.domain.dto.politics.WrongQuestionDto;
import com.tangcco.lockwell.service.QuestionService;
import com.tangcco.lockwell.service.WrongQuestionService;
import com.tangcco.lockwell.utility.JsonUtil;
import com.tangcco.lockwell.viewmodel.admin.question.QuestionVm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
/*薛明宇*/
@RestController("WrongQuestionController")
@RequestMapping(value = "/api/student/wrongQuestion")
public class WrongQuestionController {
    private final WrongQuestionService wrongQuestionService;
    @Autowired
    public WrongQuestionController(WrongQuestionService wrongQuestionService) {
        this.wrongQuestionService = wrongQuestionService;
    }

    @Autowired
    private WebContext webContext;
    @Autowired
    private QuestionService questionService;
    /**
     * 查询错题本列表
     * @return
     */
    @RequestMapping("/wrongQuestionList")
    public RestResponse<List<WrongQuestionDto>> wrongQuestionList(@RequestParam(value = "subjectId", required = false)Integer subjectId) {
        List<WrongQuestion> wrongQuestions = wrongQuestionService.selectWrongQuestionsByUserId(webContext.getCurrentUser().getUserId(),subjectId);
        List<WrongQuestionDto> result = BeanUtil.copyToList(wrongQuestions, WrongQuestionDto.class);
        if(CollectionUtils.isEmpty(result)){
            return RestResponse.ok();
        }
        result.forEach(dto -> {
            QuestionPoliticsInfoDto questionInfo = new QuestionPoliticsInfoDto();
            QuestionVm param = questionService.selectByIds(dto.getQuestionId());
            dto.setCorrectAnswer(param.getCorrect());
            dto.setSubjectId(param.getSubjectId());
            StringBuilder sb = new StringBuilder();
            if(param.getSubjectName()!=null){
                sb.append(param.getSubjectName());
            }
            if(param.getChapterName()!=null){
                sb.append("-").append(param.getChapterName());
            }
            questionInfo.setChapter(sb.toString());
            questionInfo.setSelectedYear(param.getGradeLevel());
            if(param.getTextContent() != null){
                List<QuestionPoliticsChildInfoDto> questionList = JsonUtil.toJsonListObject(param.getTextContent(), QuestionPoliticsChildInfoDto.class);
                if(CollectionUtil.isNotEmpty(questionList)){
                    questionInfo.setChildInfo(questionList);
                }
            }
            dto.setQuestionPoliticsInfoDto(questionInfo);
        });
        return RestResponse.ok(result);
    }
}
